/*
 *
 * scumix.swat.spark.Dialog
 * 对话框
 *
 * author: scumyang
 * create: 2012-12-17
 * update:
 * notes : 需导入 scumix.swat.swat3.js
 *
 */

/*
用法:
	
1.声明变量:
var dlg = new scumix.swat.spark.Dialog( null,
			{
				elem:'eDialog_NewTemplate',
				enableAnim:1,
				width:640,
				height:450,
				border: dlgfrm
			});

2.显示:
dlg.show();
dlg.show(1);

*/

scumix.swat.spark.Dialog = $.newClass();
scumix.swat.spark.Dialog.prototype =
{
	constructor: function( E, P )
	{
		this.id = $.makeGlobalId( 'SxDialog' );
		this.ElName = this.id;

		this.EContainer = $E(E) || document.body;
		this.ECoverBkg = 0;
		this.EFrame = 0;
		this.EAssocElem = P ? $E(P.elem) || 0 : 0;
		this.width = P ? P.width || 200 : 200;
		this.height = P ? P.height || 150 : 150;
		this.border = P ? P.border || 0 : 0;
		this.enableAnim = P ? $.defaultValue( P.enableAnim , 1 ) : 1;
		this.isVisible = 0;
		this.isModal = 0;

		this.initView( P );
	},

	initView: function( P )
	{
		// 创建遮挡层
		this.ECoverBkg = $.createElement({type:'div',id:this.id,css:'zIndex:65500;display:none',attr:{className:'SxDialog_Cover'}});
		$(this.ECoverBkg).css( 'opacity', this.enableAnim == 0 ? '90' : '0' );

		// 创建外框
		this.EFrame = $.createElement({type:'div',id:this.id+'_Frame',css:'zIndex:65501',attr:{className:'SxDialog_Frame',exDragable:'1'}});
		this.ECoverBkg.appendChild( this.EFrame );
		$(this.EFrame).css('width', this.width).css('height', this.height ).css( 'opacity', this.enableAnim == 0 ? '100' : '0' );

		// 创建标题栏
		var t = $.createElement({type:'div',id:this.id+'_Title',css:'zIndex:65501',attr:{className:'SxDialog_Title',exPass:'1',exDragable:'1'}});
		$(t).css('height',24).css('lineHeight','24px').css('textAlign','center').css('cursor','default');
		t.innerHTML = P.title || this.id;
		this.EFrame.appendChild( t );

		// 处理父容器
		this.EContainer.appendChild( this.ECoverBkg );
		$(this.ECoverBkg).addEvent( 'click', this.DoClickOut.bind(this));

		/*if( this.border )
		{
			this.EFrame.appendChild( this.border.getFrame() );
			$(this.border.getContent()).css('width', this.width).css('height', this.height ).show();
			$(this.border.getFrame()).css('width', this.width).css('height', this.height ).show();
		}*/

		// 处理关联内容
		if( this.EAssocElem )
		{
			with( this )
			{
				$(EAssocElem).css( 'opacity', '100' ).css( 'zIndex', 65501 ).css('height', height - 30 );
				$(EAssocElem).show();
				EFrame.appendChild( EAssocElem );
				//$(EAssocElem).css('width', width-5);
			//$(this.EFrame).css( 'width', $.browser.msie ? this.EAssocElem.currentStyle.width : getComputedStyle(this.EAssocElem).width );
			//$(this.EFrame).css( 'height', $.browser.msie ? this.EAssocElem.currentStyle.height : getComputedStyle(this.EAssocElem).height );
			}
		}

		return this;
	},

	show: function( __modal )
	{
		if( this.EAssocElem )
		{
			$(this.ECoverBkg).show().resizeTo( document.body.clientWidth, document.body.clientHeight );
			$(this.EFrame).show().alignTo( 0, 'center' );
			this.isVisible = 1;
			this.isModal = __modal || 0;

			if( this.enableAnim > 0 )
			{
				var I = this;
				$(I.ECoverBkg).anim({opacity:80},5);
				$(I.EFrame).anim({opacity:100},5);
			}
		}

		return this;
	},

	hide: function()
	{
		if( this.EAssocElem )
		{
			this.isVisible = 0;
			if( this.enableAnim > 0 )
			{
				var I = this;
				$(I.ECoverBkg).anim({opacity:1},10,function(){$(I.ECoverBkg).hide()});
				$(I.EFrame).anim({opacity:1},10,function(){$(I.EFrame).hide()});
			}
			else
			{
				$(this.ECoverBkg).hide();
				$(this.EFrame).hide();
			}
		}

		return this;
	},

	close: function()
	{
		this.hide();
	},

	// 界外点击
	DoClickOut: function( E )
	{
		if( !this.isVisible || this.isModal ) return;
		var e = $.getTarget(E);
		//if( $(this.ECoverBkg).ptIn( E ) && e != this.EAssocElem ) this.hide();
		if( e == this.ECoverBkg ) this.close();
	}
}